import pandas as pd
import uuid
import json
import time
import mysql.connector
import tos
import logging
import  random
from bytehouse_driver import Client
import numpy as np


def add_to_bytehouse_dict(dict):
    HOST="bytehouse-cn-beijing.volces.com"
    PORT="19000"
    API_KEY="q2p9nLj7tq:TKOCmgrMKp"
    # 配置数据库连接信息
    DATABASE="dwd"
    client = Client.from_url('bytehouse://{}:{}/?user=bytehouse&password={}&database={}&secure=true'.format(HOST, PORT, API_KEY, DATABASE))
    client.execute("INSERT INTO ods.ods_trigger_sc  VALUES", [
        [dict['vehicle_id'], dict['scid'], dict['start_time'], dict['path'], dict['plat'], dict['project'],dict['day'],dict['isdelete']
            , dict['pt_date'], dict['pt_hour'],dict['pt_vehicleseries'],dict['pt_plat']
        ]])
    return
def get_emergency_steering_scid_time(df_can_100ms_save_path, vehicle_id, bagid, daystr, hourstr):
    try:
        df_can_100ms = pd.read_pickle(df_can_100ms_save_path)
    except Exception as e:
        print('data report read error, ', str(e))

    df_100ms_list = df_can_100ms[
        ['start_time_str', 'path', 'nsecs', 'VLCCDHypotheses_Hypothesis_0_fTTC', 'VLCCDHypotheses_Hypothesis_0_fDistX',
         'VLCCDHypotheses_Hypothesis_0_fDistY',
         'VLCCDHypotheses_Hypothesis_0_fVrelX', 'VLCCDHypotheses_Hypothesis_0_fVrelY', 'IDB3_VehicleSpd',
         'ACU2_LongAccSensorValue', 'ACU2_LatAccSensorValue',
         'ACU2_VehicleDynYawRate', 'IDB1_BrakePedalApplied', 'EPS1_SteerAngleSpd',
         'CamLaneData_CourseInfo_1_CourseInfoSegNear_f_C0',
         'ADCS8_NPilot_SysState', 'ADCS8_NNPSysState', 'ICU2_Odometer', 'EPS1_TorsionBarTorque', 'CS1_GearPositionReqSt','IDB5_AEBactive',
         'ADCS2_AEB_DBSLevel','ADCS8_ACCState','VLCCDHypotheses_Hypothesis_0_eEBAObjectClass','AccDisplayObj_CONTROL_ACCEL','ADCS2_EPS_LDPState','BDCS1_TurnLightSW'
         ]].values.tolist()

    found_path=""
    found_start_time=""
    for index, element in enumerate(df_100ms_list):
        start_time_str=element[0]
        path=element[1]
        nsecs=element[2]
        ADCS8_NPilot_SysState=element[15]
        ADCS8_NNPSysState=element[16]
        ADCS2_EPS_LDPState=element[25]
        BDCS1_TurnLightSW=element[26]
        IDB3_VehicleSpd = element[8]
        ACU2_VehicleDynYawRate = element[11]
        EPS1_SteerAngleSpd = element[13]


        if (ADCS8_NNPSysState==2 or ADCS8_NPilot_SysState==2 or ADCS2_EPS_LDPState==2) and (abs(ACU2_VehicleDynYawRate)>2) and (BDCS1_TurnLightSW==0) and \
            ((IDB3_VehicleSpd>=100 and abs(EPS1_SteerAngleSpd)>16)  or \
             (IDB3_VehicleSpd>=80 and IDB3_VehicleSpd<100 and abs(EPS1_SteerAngleSpd)>22) or \
             (IDB3_VehicleSpd>=60 and IDB3_VehicleSpd<80 and abs(EPS1_SteerAngleSpd)>25) or \
             (IDB3_VehicleSpd>=50 and IDB3_VehicleSpd<60 and abs(EPS1_SteerAngleSpd)>40) or\
             (IDB3_VehicleSpd>=40 and IDB3_VehicleSpd<50 and abs(EPS1_SteerAngleSpd)>50) or\
             (IDB3_VehicleSpd>30 and IDB3_VehicleSpd<40 and abs(EPS1_SteerAngleSpd)>60)
            ):
            found_index=index
            found_path=path
            found_start_time=start_time_str

            break;

    logging.info("found_index: " + str(found_index))
    if found_index==  -9999:
        dict_scid = {}
        dict_scid["vehicle_id"] = vehicle_id
        dict_scid["scid"] = bagid
        dict_scid["start_time"] =""
        dict_scid["path"] = df_100ms_list[0][1]
        dict_scid["plat"] = "TDA4"
        dict_scid["project"] = "EP36"
        dict_scid["day"] = daystr
        isdelete = random.randint(0, 9)
        dict_scid["isdelete"] = isdelete
        dict_scid["pt_date"] = daystr
        dict_scid["pt_hour"] = hourstr
        dict_scid["pt_vehicleseries"] = 'EP36'
        dict_scid["pt_plat"] = 'TDA4'
        add_to_bytehouse_dict(dict_scid)
    else:
        dict_scid = {}
        dict_scid["vehicle_id"] = vehicle_id
        dict_scid["scid"] = bagid
        dict_scid["start_time"] = found_start_time
        dict_scid["path"] = found_path
        dict_scid["plat"] = "TDA4"
        dict_scid["project"] = "EP36"
        dict_scid["day"] = daystr
        isdelete = random.randint(0, 9)
        dict_scid["isdelete"] = isdelete
        dict_scid["pt_date"] = daystr
        dict_scid["pt_hour"] = hourstr
        dict_scid["pt_vehicleseries"] = 'EP36'
        dict_scid["pt_plat"] = 'TDA4'
        add_to_bytehouse_dict(dict_scid)
















